package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Dto.SetmealDto;
import com.itheima.common.R;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetMealDishService;
import com.itheima.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private SetMealDishService setMealDishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;



    /**
     * 新增套餐
     * @param setMealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setMealCache",allEntries = true) //清除setMealCache名称下，所有的缓存数据
    public R<String> save(@RequestBody SetmealDto setMealDto) {
        log.info("套餐信息:{}",setMealDto);

        setMealService.saveWithDish(setMealDto);

        return R.success("新增套餐成功");
    }

    /**
     * 分页查询数据
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R queryPage(int page,int pageSize,String name) {

        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Setmeal::getName,name);
        setMealService.page(pageInfo,lqw);

        long total = pageInfo.getTotal();
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> collect = records.stream().map(setMeal -> {
            SetmealDto setMealDto = new SetmealDto();
            BeanUtils.copyProperties(setMeal, setMealDto);
            Long categoryId = setMealDto.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setMealDto.setCategoryName(category.getName());
            return setMealDto;
        }).collect(Collectors.toList());

        Map map = new HashMap();
        map.put("total",total);
        map.put("records",collect);

        return R.success(map);
    }

    /**
     * 根据id删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setMealCache",allEntries = true) //清除setMealCache名称下，所有的缓存数据
    public R<String> delete(@RequestParam List<Long> ids) {
        setMealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * 对菜品批量或者单个进行停售和起售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R updateStatusById(@PathVariable("status") Integer status,@RequestParam List<Long> ids){

        setMealService.updateSetMealStatusById(status,ids);

        return R.success("修改菜品状态成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setMealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(null!=setmeal.getCategoryId(),Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(null!=setmeal.getStatus(),Setmeal::getStatus,setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setMealService.list(lqw);
        return R.success(list);
    }
}
